package com.bosch.in.rtp1.ept.manager.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bosch.in.rtp1.ept.dao.HldDurByMonDiagramDao;
import com.bosch.in.rtp1.ept.manager.HldDurByMonthManager;
import com.bosch.in.rtp1.ept.model.dto.HldDurByMonth;
import com.bosch.in.rtp1.ept.model.technical.Condition4HldDurByMonthDiagram;
import com.bosch.in.rtp1.ept.util.DaoFactory;
import com.bosch.in.rtp1.ept.util.ehcache.EhcacheFactory;

/**
 * Concrete implementation of HldDurByMonthManager which provides service to manipulate 
 * data, delegate data source retrieval and transaction management to 
 * DBConnectionManager attribute.  
 * @author LIV1HC
 *
 */

public class HldDurByMonthManagerImpl implements HldDurByMonthManager{

	/**
	 * used to manage connection
	 */
	private DBConnectionManager dbManager;
	
	/**
	 * dao object
	 */
	private HldDurByMonDiagramDao hDiagramDao;
	
	public HldDurByMonthManagerImpl(){
	}

	@Override
	public List<HldDurByMonth> getHldDurByMonths(
			Condition4HldDurByMonthDiagram condition4HldDurByMonthDiagram) throws SQLException {
		try {
				return this.getHldDurList(condition4HldDurByMonthDiagram);
			} 
		finally {
			if (dbManager != null) {
				dbManager.close();
			}
		}
	}
	
	/***********PRIVATE METHOD****************/
	
	/**
	 * lazy initialize connection when we really need to interact with database and in case 
	 * data can't be found in caching system.
	 * @throws SQLException
	 */
	private void init() throws SQLException {
		dbManager = new DBConnectionManager();
		hDiagramDao = DaoFactory.getInstance().getHldDurByMonthDao(dbManager.getConnection());
	}
	
	/**
	 * Try to get the list of HldDurByMonth from cache based on Condition4HldDurByMonthDiagram
	 * @param Condition4HldDurByMonthDiagram
	 * @return  + List<HldDurByMonth> if it is found
	 * 			+ NULL or empty list if it is not found
	 */
	private List<HldDurByMonth> getHldDurListFromCache(Condition4HldDurByMonthDiagram cMonthDiagram)
	{
		//try to load HldDurByMonth from cache
		List<HldDurByMonth> hldDurByMonths = EhcacheFactory.getHldDurDiagramcacheWrapper().get(cMonthDiagram.getParameterValues().toString());
		
		// if this loading for one month or for exporting, try to get cache of all diagrams
		if (hldDurByMonths == null && (cMonthDiagram.getPriority()>0 || cMonthDiagram.getMonth()>0))
		{
			cMonthDiagram.builParameterValuesList(0,0);
			List<HldDurByMonth> hldDurByMonthsCache = EhcacheFactory.getHldDurDiagramcacheWrapper().get(cMonthDiagram.getParameterValues().toString());
			
			if (hldDurByMonthsCache != null )
			{
				hldDurByMonths = new ArrayList<HldDurByMonth>();
				// if this loading for one month or for exporting, remove unused records
				int i=0;
				while (i<hldDurByMonthsCache.size())
				{
					HldDurByMonth hByMonth = hldDurByMonthsCache.get(i);
					if (
							(hByMonth.getPriority() == cMonthDiagram.getPriority() || cMonthDiagram.getPriority()== 0)
							&& hByMonth.getMonth() == cMonthDiagram.getMonth() 
						)
					{
						hldDurByMonths.add(hByMonth);
					}
					++i;
				}
			}
		} 
		return hldDurByMonths;
	}
	
	/**
	 * Try to get the list of HldDurByMonth from database based on Condition4HldDurByMonthDiagram
	 * @param Condition4HldDurByMonthDiagram
	 * @return List<HldDurByMonth>
	 * @throws SQLException
	 */
	private List<HldDurByMonth> getHldDurListFromDao(Condition4HldDurByMonthDiagram cMonthDiagram) throws SQLException
	{
		List<HldDurByMonth> hldDurByMonths = new ArrayList<HldDurByMonth>();
		
		// lazy init connection
		init();
		// load fshRcpChartValues from database
		if (cMonthDiagram.getPriority()>0 || cMonthDiagram.getMonth()>0)
		{
			cMonthDiagram.builParameterValuesList();
		}
		hldDurByMonths = hDiagramDao.getHldDurChartByMonth(cMonthDiagram);
			
		//put fshRcpChartValues list into cache
		if (hldDurByMonths.size()>0)
		{
			EhcacheFactory.getHldDurDiagramcacheWrapper().put(cMonthDiagram.getParameterValues().toString(),hldDurByMonths);
		}
		
		return hldDurByMonths;
		
	}
	
	/**
	 * Try to load the list of HldDurByMonth from cache first. 
	 * If it is not found, try loading from database
	 * @param Condition4HldDurByMonthDiagram
	 * @return
	 * @throws SQLException
	 */
	private List<HldDurByMonth> getHldDurList(Condition4HldDurByMonthDiagram cMonthDiagram) throws SQLException
	{
		// try to load from cache
		List<HldDurByMonth> hldDurByMonths = getHldDurListFromCache(cMonthDiagram);
		if (hldDurByMonths == null || hldDurByMonths.size()==0)
		{
			// try to load from dao
			hldDurByMonths = getHldDurListFromDao(cMonthDiagram);
		}
		
		return hldDurByMonths;
	}

}
